from django.db import models
from datetime import datetime


# Create your models here.

# 员工账号信息模型
class User(models.Model):
    username = models.CharField(verbose_name='员工账号', max_length=50)
    nickname = models.CharField(verbose_name='员工昵称', max_length=50)
    password_hash = models.CharField(verbose_name='密码', max_length=100)
    password_salt = models.CharField(verbose_name='密码干扰值', max_length=50)
    status = models.IntegerField(verbose_name='状态', default=1)  # 1正常/2禁用/9删除/6管理员
    create_at = models.DateTimeField(verbose_name='创建时间', default=datetime.now)
    update_at = models.DateTimeField(verbose_name='修改时间', default=datetime.now)

    def toDict(self):
        return {
            'id': self.id,
            'username': self.username,
            'nickname': self.nickname,
            'password_hash': self.password_hash,
            'password_salt': self.password_salt,
            'status': self.status,
            'create_at': str(self.create_at),
            'update_at': str(self.update_at),
        }

    class Mate:
        db_table = 'user'


# 店铺
class Shop(models.Model):
    name = models.CharField(verbose_name='店铺名称', max_length=255)
    cover_pic = models.CharField(verbose_name='封面图片', max_length=255)
    banner_pic = models.CharField(verbose_name='图标logo', max_length=255)
    address = models.CharField(verbose_name='店铺地址', max_length=255)
    phone = models.CharField(verbose_name='联系电话', max_length=255)
    status = models.IntegerField(verbose_name='店铺状态', default=1)  # 1正常/2暂停/9删除
    create_at = models.DateTimeField(verbose_name='创建时间', default=datetime.now)
    update_at = models.DateTimeField(verbose_name='修改时间', default=datetime.now)

    def toDict(self):
        shopname = self.name.split('-')
        return {
            'id': self.id,
            'name': shopname[0],
            'shop': shopname[1],
            'cover_pic': self.cover_pic,
            'banner_pic': self.banner_pic,
            'address': self.address,
            'phone': self.phone,
            'status': self.status,
            'create_at': str(self.create_at),
            'update_at': str(self.update_at),
        }

    class Mate:
        db_table = 'shop'


# 菜品分类
class Category(models.Model):
    shop_id = models.IntegerField(verbose_name='店铺ID')
    name = models.CharField(verbose_name='分类名称', max_length=50)
    status = models.IntegerField(verbose_name='分类状态', default=1)  # 1正常/9删除
    create_at = models.DateTimeField(verbose_name='创建时间', default=datetime.now)
    update_at = models.DateTimeField(verbose_name='修改时间', default=datetime.now)

    class Meta:
        db_table = 'category'


# 菜品信息
class Product(models.Model):
    shop_id = models.IntegerField(verbose_name='店铺ID')
    category_id = models.IntegerField(verbose_name='菜品分类ID')
    cover_pic = models.CharField(verbose_name='菜品图片', max_length=255)
    name = models.CharField(verbose_name='菜品名称', max_length=255)
    price = models.FloatField(verbose_name='菜品单价')
    status = models.IntegerField(verbose_name='店铺状态', default=1)  # 1正常/2停售/9删除
    create_at = models.DateTimeField(verbose_name='创建时间', default=datetime.now)
    update_at = models.DateTimeField(verbose_name='修改时间', default=datetime.now)

    def toDict(self):
        return {
            'id': self.id,
            'shop_id': self.shop_id,
            'category_id': self.category_id,
            'cover_pic': self.cover_pic,
            'name': self.name,
            'price': self.price,
            'status': self.status,
            'create_at': str(self.create_at),
            'update_at': str(self.update_at),
        }

    class Mate:
        db_table = 'product'


# 会员
class Member(models.Model):
    nickname = models.CharField(verbose_name='昵称', max_length=50)
    avatar = models.CharField(verbose_name='头像', max_length=255)
    password = models.CharField(verbose_name='密码', default='', max_length=255)
    mobile = models.CharField(verbose_name='电话', max_length=50)
    status = models.IntegerField(verbose_name='状态', default=1)  # 1正常/2禁用/9删除
    create_at = models.DateTimeField(verbose_name='创建时间', default=datetime.now)
    update_at = models.DateTimeField(verbose_name='修改时间', default=datetime.now)

    def toDict(self):
        return {
            'id': self.id,
            'nickname': self.nickname,
            'avatar': self.avatar,
            'mobile': self.mobile,
            'password': self.password,
            'status': self.status,
            'create_at': str(self.create_at),
            'update_at': str(self.update_at),
        }

    class Mate:
        db_table = 'member'


# 会员详细信息
# class Detailed_Member(models.Model):
#     member_id = models.IntegerField(verbose_name='会员ID')
#     brief_introduction = models.CharField(verbose_name='简介', default='', max_length=512)
#     gender = models.IntegerField(verbose_name='性别', default=1)  # 1男/2女
#     birthday = models.DateTimeField(verbose_name='创建时间', default=datetime.now)
#     ip_address = models.CharField(verbose_name='IP地址', default='', max_length=255)


# 订单
class Orders(models.Model):
    shop_id = models.IntegerField(verbose_name='店铺ID')
    member_id = models.IntegerField(verbose_name='会员ID')
    user_id = models.IntegerField(verbose_name='操作员ID')
    money = models.FloatField(verbose_name='金额')
    status = models.IntegerField(verbose_name='订单状态', default=1)  # 1进行中/2无效/3已完成
    payment_status = models.IntegerField(verbose_name='支付状态', default=1)  # 1未支付/2已支付/3已退款
    create_at = models.DateTimeField(verbose_name='创建时间', default=datetime.now)
    update_at = models.DateTimeField(verbose_name='修改时间', default=datetime.now)

    class Meta:
        db_table = 'orders'


# 订单详情
class OrdersDetail(models.Model):
    order_id = models.IntegerField(verbose_name='订单ID')
    # product_id = models.IntegerField(verbose_name='菜品ID')
    product = models.ForeignKey('Product', on_delete=models.CASCADE)  # 多对一
    product_name = models.CharField(verbose_name='菜品名称', max_length=50)
    price = models.IntegerField(verbose_name='单价')
    quantity = models.IntegerField(verbose_name='数量')
    status = models.IntegerField(verbose_name='状态', default=1)  # 1正常/9删除

    class Meta:
        db_table = 'order_Detail'


# 支付信息
class Payment(models.Model):
    order_id = models.IntegerField(verbose_name='订单ID')
    member_id = models.IntegerField(verbose_name='会员ID')
    money = models.FloatField(verbose_name='支付金额')
    type = models.IntegerField(verbose_name='付款方式')  # 1会员付款/2收银收款
    bank = models.IntegerField(verbose_name='收款银行渠道', default=1)  # 1微信/2余额/3现金/4支付宝
    status = models.IntegerField(verbose_name='支付状态', default=1)  # 1未支付/2已支付/3已退款
    create_at = models.DateTimeField(verbose_name='创建时间', default=datetime.now)
    update_at = models.DateTimeField(verbose_name='修改时间', default=datetime.now)

    class Meta:
        db_table = 'payment'
